<template>
  <div class="xtx-checkbox" @click="checkClick">
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <span><slot/></span>
  </div>
</template>
<script>
import { ref, watch } from 'vue'

export default {
  name: 'XtxCheckbox',
  props: {
    modelValue: {
      type: Boolean,
      default: false
    }
  },
  setup (props, { emit }) {
    // 创建一个与modelValue代理
    const checked = ref(false)

    function checkClick () {
      //  取反并发送事件改modelVale
      checked.value = !checked.value
      emit('update:modelVale', checked)
    }
    watch(() => props.modelValue, newVal => checked.value = newVal,{immediate:true})
    return { checkClick,checked }
  }

}
</script>
<style scoped lang="less">
.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;

  .icon-checked {
    color: @xtxColor;

    ~ span {
      color: @xtxColor;
    }
  }

  i {
    position: relative;
    top: 1px;
  }

  span {
    margin-left: 2px;
  }
}
</style>
